<?php

namespace app\controllers;


use app\models\Admin;
use yii\db\Query;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;


class AdminBaseController extends Controller {

    public $layout = false;
    public $enableCsrfValidation = false;

   public function init(){
       parent::init();
       $identity=\Yii::$app->user->identity;
       if (is_null($identity)){
           \Yii::$app->user->loginRequired();
       }
   }

    //上传图片
    public function actionUpload(){
      if (\Yii::$app->request->isPost){
          $model = new UploadForm();

          if (\Yii::$app->request->isPost) {
              $model->imageFile = $model->imageFile=UploadedFile::getInstanceByName('imageFile');
              $res=$model->upload();
              if ($res['code']) {
                  $data['url']=$res['url'];
                  $data['create_time']=time();
                  \Yii::$app->db->createCommand()->insert('yii_picture',$data)->execute();
                  $id=\Yii::$app->db->getLastInsertID();

                  // 文件上传成功
                  return json_encode(['code'=>1,'url'=>$data['url'],'img_id'=>$id]);
              }else{
                  return json_encode(['code'=>0,'上传失败']);
              }
          }
      }
    }

    //获取图片
    public function GetImg($id){
        $list=(new Query())->select('url')->from('yii_picture')->where(array('id'=>$id))->one();
        if ($list){
            return $list['url'];
        }
    }

    //删除图片文件
    public function DelPicture($img_id){
        \Yii::$app->db->createCommand()->delete('yii_picture',array('id'=>$img_id))->execute();
    }

    //删除目录文件
    public function DelFile($img_id){
        $path=substr($this->GetImg($img_id),1);
        unlink($path);
    }

    //获取Post请求ID
    public function PostId(){
        $id=(int)\Yii::$app->request->post()['id'];
        return $id;
    }

    //获取Get请求ID
    public function GetId(){
        $id=(int)\Yii::$app->request->get()['id'];
        return $id;
    }


    //post请求
    public function post(){
        if (\Yii::$app->request->isPost){
            return true;
        }else{
            return false;
        }
    }

    //获取用户信息
    public function GetUser($user_id){
        $where['user_id']=$user_id;
        $list=(new Query())->select('nickName,phone,gender,wechat,birthday,work_time')->from('yii_user')->where($where)->one();
        return $list;
    }

    //获取用户信息
    public function GetWork($id){
        $where['id']=$id;
        $list=(new Query())->select('*')->from('yii_work')->where($where)->one();
        return $list;
    }


}
